package com.ruoyi.web.controller.tool;

import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;
import com.sini.com.spire.doc.Document;
import com.sini.com.spire.doc.FileFormat;

import java.io.*;

public class WordToPdf {
    public static String WordToPdf(String wordPath) {
        File inputWord = new File(wordPath);
        String pdfPath = wordPath.replace(".docx", ".pdf").replace(".doc", ".pdf")
        ;
        File outputFile = new File(pdfPath);
        try {
            InputStream docxInputStream = new FileInputStream(inputWord);
            OutputStream outputStream = new FileOutputStream(outputFile);
            IConverter converter = LocalConverter.builder().build();
            converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
            docxInputStream.close();
            outputStream.close();
            converter.shutDown();
            System.out.println("success");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pdfPath;
    }
}
